Skip to content

Reject incomplete density annotations in formula parser - #145

Open
swf1996120 wants to merge 1 commit into
python-periodictable:masterfrom
swf1996120:fix-incomplete-density-annotation
Open

swf1996120 wants to merge 1 commit into
python-periodictable:masterfrom
swf1996120:fix-incomplete-density-annotation

Conversation

@swf1996120

Copy link
Copy Markdown

Summary

This report comes from an LLM-assisted testing study of chemical software. I ran and minimized the reproducer locally against the current release and checked the repository issue tracker for duplicates.

formula("H2O@") silently accepts an incomplete density annotation and sets the formula density to 1, instead of rejecting the malformed input or leaving the density unspecified.

Reproducer

from periodictable import formula

plain = formula("H2O")
malformed = formula("H2O@")

print(plain.density)      # None
print(malformed.density)  # 1
print(malformed)          # H2O

For comparison, malformed values such as formula("H2O@.") and formula("H2O@1q") raise an exception, while the missing numeric value after @ is interpreted as the default count 1.

Expected behavior

A trailing @ without a density should raise a parse error. It should not silently change the semantic value of the formula by assigning density 1.

Environment

  • periodictable 2.1.0
  • Python 3.13

The behavior is caused by the density grammar using number, whose optional count defaults to 1, after the literal @.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant